using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using QLNhaSach.BUS.DTO;

namespace QLNhaSach.DAO
{
    public class KhachHangDAO
    {
        //Hien thi danh sach khach hang
        public static DataTable DSKH()
        {
            string sql = "SELECT * FROM KhachHang";
            return Config.AccessHelper.ExecuteQuery(sql);
        }

        //Hien thi cong no mot khach hang
        public static DataTable XemCongNoKH(KhachHangDTO kh)
        {
            string sql = "SELECT * FROM KhachHang" + " where MaKH=" + kh.MaKH;
            return Config.AccessHelper.ExecuteQuery(sql);
        }

        //Them thong tin khach hang
        public static bool ThemTTKH(KhachHangDTO kh)
        {
            try
            {
                //Tao cau truy van va thuc thi cau truy van
                string sql = "INSERT INTO KhachHang(TenKH,DiaChi,DienThoai,TienNoDau,NoPhatSinh,DaThanhToan,TienNoCuoi) VALUES('" +
                kh.TenKH + "','" +
                kh.DiaChi + "','" +
                kh.DienThoai + "'," +
                kh.TienNoDau + "," +
                kh.NoPhatSinh + "," +
                kh.DaThanhToan + "," +
                kh.TienNoCuoi + ")";
                Config.AccessHelper.ExecuteNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Cap nhat thong tin mot khach hang
        public static bool CapNhatTTKH(KhachHangDTO kh)
        {
            try
            {
                string sql = "UPDATE KhachHang SET DiaChi='" + kh.DiaChi + "',DienThoai='" + kh.DienThoai + "' where MaKH=" + kh.MaKH;
                Config.AccessHelper.ExecuteNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Cap nhap cong no khach hang
        public static bool CapNhapCongNo(KhachHangDTO kh)
        {
            try
            {
                string sql = "UPDATE KhachHang SET TienNoDau = " + kh.TienNoDau + ",";
                sql += " NoPhatSinh = " + kh.NoPhatSinh + ", DaThanhToan = " + kh.DaThanhToan + ",";
                sql += " TienNoCuoi =" + kh.TienNoCuoi + " WHERE (MaKH = " + kh.MaKH + ")";
                Config.AccessHelper.ExecuteNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
